<?php
namespace Home\Controller;

use Home\Model\UserAddressModel;
class UcenterController extends CommonController{
    private $user;
    public function _initialize(){
        $this->user = session('user');
    }
	public function index(){

	    $this->display();
    }
    //订单列表
    public function orderList(){
        $where = array();
        $type = I('type') ? I('type') : '1';
        switch ($type) {
            case '1':$where['all_status'] = array('elt','3');
            $this->assign('title','待审核订单');break;//待审核
            case '2':$where['all_status'] = '4';
            $this->assign('title','待支付订单');break;//待支付
            case '3':$where['all_status'] = '5';
            $this->assign('title','支付审核中订单');break;//支付审核
            case '4':$where['all_status'] = '10';
            $this->assign('title','办理中订单');break;//办理中
            case '5':$where['all_status'] = array('egt','14');
            $this->assign('title','已完成订单');break;//客户交接中或已完成
            default:$where['all_status'] = array('elt','3');
            $this->assign('title','待审核订单');
        }
        $where['user_id'] = $this->user['user_id'];
        $order = D('OrderInfo')->relation(true)->where($where)->order('add_time desc')->select();
        foreach ($order as $key=>$val){
            $contain_goods = array();
            if(!empty($val['order_goods'])){
                foreach ($val['order_goods'] as $v){
                    if(empty($order[$key]['goods_name'])) $order[$key]['goods_name'] = $v['goods_name'];
                    $contain_goods[] = $v['goods_name'];
                }
            }
            $order[$key]['contain_goods'] = implode('、', $contain_goods);
        }
        $this->assign('order_data',$order);
        $this->assign('type',$type);
        $this->display();
    }
    
    public function orderDetail(){
        $order_id = (int)I('order_id');
        if(empty($order_id)) $this->error('无效参数！');
        //order 数据
        $where = array();
        $where['order_id'] = $order_id;
        $where['user_id'] = $this->user['user_id'];
        $order_data = D('OrderInfo')->relation(true)->where($where)->find();
        $order_goods = $order_data['order_goods'];
        $contain_goods = array();
        foreach ($order_goods as $key=>$val){
            if($val['extension_code'] == 'package_buy'){
                $package_goods = D('PackageGoods')->relation(true)->where("package_id='{$val['goods_id']}'")->order('is_flow_type asc')->select();
                var_dump($package_goods);exit;
            }
            $order_data['is_flow_type'] = 1;
            $order_goods[$key]['user_data'] = unserialize($val['user_data']);
            $contain_goods[] = $val['goods_name'];
            if(empty($order_data['goods_name'])) {
                $order_data['goods_name'] = $val['goods_name'];
            }
            if(empty($order_data['is_flow_type'])){
                
            }
        }
        $order_data['contain_goods'] = $contain_goods ? implode('、', $contain_goods) : '';
        //省份
        $provinces = D('Region')->where(array('region_type'=>'1'))->select();
        $order_data['province'] && $citys = D('Region')->where("parent_id='{$order_data['province']}'")->select();
        $order_data['city'] && $districts = D('Region')->where("parent_id='{$order_data['city']}'")->select();
        //以往发票
        $inv_data = D('OrderInfo')->where("user_id='{$this->user['user_id']}' and order_id<>'$order_id' and inv_payee<>''")->group("inv_payee")->find();
        
        $this->assign('inv_data',$inv_data);
        $this->assign('citys',$citys);
        $this->assign('districts',$districts);
        $this->assign('provinces',$provinces);
        $this->assign('order_goods',$order_goods);
        $this->assign('order_data',$order_data);
        $this->display();
    }

    public function deleteOrder(){
        $order_id = (int)I('post.order_id');
        $res = D('OrderInfo')->where(array('order_id'=>$order_id))->delete();
        $res && D('OrderGoods')->where(array('order_id'=>$order_id))->delete();
        $res ? $this->success('删除成功！') : $this->error('删除失败！');
    }
    
    //更新订单的收件人信息
    public function saveConsignee(){
        $data = I('post.');
        $order_id = (int)$data['order_id'];
        unset($data['order_id']);
        $result = D("OrderInfo")->where("order_id='$order_id'")->save($data);
        $result ? $this->success('保存成功！'):$this->error('保存失败！');
    }
    
    //
    public function saveInv(){
        $data = I('post.');
        $order_id = (int)$data['order_id'];
        unset($data['order_id'],$data['inv_old']);
        $result = D("OrderInfo")->where("order_id='$order_id'")->save($data);
        $result ? $this->success('保存成功！'):$this->error('保存失败！');
    }
    
    public function editInfo(){
        if(IS_POST){
            //用户信息
            $user_info = I('post.info');
            $user_info['user_data'] = serialize(I('post.user_data'));
            D('Users')->where(array('user_id'=>$this->user['user_id']))->save($user_info);
            //地址信息
            $addr_data = I('post.addr');
            if($addr = D('UserAddress')->check_default($this->user['user_id'])){
                D('UserAddress')->where(array('address_id'=>$addr['address_id']))->save($addr_data);
            }else{
                $addr_data['user_id'] = $this->user['user_id'];
                $addr_data['mobile'] = $user_info['home_phone'];
                $addr_data['consignee'] = $user_info['name'];
                $addr_data['email'] = $user_info['email'];
                $addr_data['is_default'] = '1';
                D('UserAddress')->add($addr_data);
            }
            $company_info = I('post.cp');
            if($cp_data = D('Company')->check_company($this->user['user_id'])){
                D('Company')->where(array('cp_id'=>$cp_data['cp_id']))->save($company_info);
            }else{
                $company_info['user_id'] = $this->user['user_id'];
                D('Company')->add($company_info);
            }
            //更新session中的数据
            session('user',D('Users')->where(array('user_id'=>$this->user['user_id']))->find());
            $this->success('保存成功！',U('index'));
        }else{
            $this->assign('user_info',$this->user);
            $this->assign('user_data',unserialize($this->user['user_data']));
            $this->assign('addr',D('UserAddress')->check_default($this->user['user_id']));
            $this->assign('cp_data',D('Company')->where(array('user_id'=>$this->user['user_id']))->find());
            $this->display();
        }
    }
    public function editPass(){
        if(IS_POST){
            $data = I('post.');
            if(!R('Login/check_user',array($this->user['user_name'],$data['old_password']))){
                $this->error('原密码错误！');
            }
            //验证两次输入的密码是否一致
            if($data['new_password'] != $data['confirm_password'])$this->error('两次输入的密码不一致！');
            
            $update_data['password'] = compile_password(array('password'=>$data['new_password'],'ec_salt'=>$user['ec_salt']));
            $res = D('Users')->where(array('user_id'=>$this->user['user_id']))->save($update_data);
            $res ? $this->success('密码修改成功',U('index')):$this->error('修改失败！');
        }else{
            $this->display();
        }
    }
    //地址列表
    public function address(){
        $this->assign('address_list',D('UserAddress')->address_list($this->user['user_id']));
        $this->display();
    }
    //编辑地址
    public function editAddress(){
        $region = D('Region');
        $userAddress = D('userAddress');
        if(IS_POST){
            $data = I('post.');
            $address_id = $data['address_id'];
            unset($data['address_id']);
            if(!$res = $userAddress->create($data))$this->error($userAddress->getError());
            if(empty($address_id)){
                $data['user_id'] = $this->user['user_id'];
                $result = $userAddress->add($data);
            }else{
                $result = $userAddress->where(array('address_id'=>$address_id))->save($data);
            }
            $result ? $this->success('保存成功！',U('address')) : $this->error('保存失败！');
        }else{
            $address_data = array();
            $provinces = $region->where(array('region_type'=>'1'))->select();
            if($address_id = intval(I('get.address_id'))){
                $address_data = $userAddress->where("address_id='$address_id'")->find();
                $address_data['province'] && $citys = $region->where("parent_id='{$address_data['province']}'")->select();
                $address_data['city'] && $districts = $region->where("parent_id='{$address_data['city']}'")->select();
                $this->assign('citys',$citys);
                $this->assign('districts',$districts);
            }
            $this->assign('provinces',$provinces);
            $this->assign('address_data',$address_data);
            $this->display();
        }
    }
    
    public function getCitys(){
        $region = D('Region');
        $province = (int)I('post.province');
        $citys = $region->where(array('parent_id'=>$province))->select();        
        $districts = $region->where(array('parent_id'=>$citys[0]['region_id']))->select();
        $this->success(array('citys'=>$citys,'districts'=>$districts));
    }
    
    public function getDistricts(){
        $region = D('Region');
        $city = (int)I('post.city');
        $districts = $region->where(array('parent_id'=>$city))->select();
        $this->success($districts);
    }
    
    //设置默认地址
    public function setDefaultAddress(){
        $address_id = (int)I('post.address_id');
        $address = D('UserAddress');
        $where = array('user_id'=>$this->user['user_id']);
        $address->where($where)->save(array('is_default'=>'2'));
        $where['address_id'] = $address_id;
        $address->where($where)->save(array('is_default'=>'1'));
        $this->success('');
    }
    
    public function deleteAddress(){
        $address_id = (int)I('post.address_id');
        $result = D('UserAddress')->where(array('address_id'=>$address_id))->delete();
        $result ? $this->success('') : $this->error('删除失败！');
    }
    
    public function loginOut(){
        session('user',null);
        $this->redirect('Login/index');
    }
    
    public function invoice(){
        $invoice_data = D('OrderInfo')->relation(true)->where("user_id='{$this->user['user_id']}' and inv_payee<>''")->order('add_time')->select();
        foreach ($invoice_data as $key=>$val){
            $order_goods = $val['order_goods'];
            $contain_goods = array();
            foreach ($order_goods as $k=>$v){
                if(empty($invoice_data[$key]['goods_name']))$invoice_data[$key]['goods_name'] = $v['goods_name']; 
                $contain_goods[] = $v['goods_name'];
            }
            $invoice_data[$key]['contain_goods'] = implode('、', $contain_goods);
        }
        $this->assign('invoice_data',$invoice_data);
        $this->display();
    }
    
    public function invoiceDetail(){
        $order_id = (int)I('order_id');
        $where = array();
        $where['user_id'] = $this->user['user_id'];
        $where['order_id'] = $order_id;
        $data = D('OrderInfo')->relation(true)->where($where)->find();
        $this->assign('data',$data);
        $this->display();
    }
    
    public function refundInv(){
        $data = I('post.');
        $order_id = (int)$data['order_id'];
        unset($data['order_id']);
        $result = D('OrderInfo')->where("order_id='$order_id'")->save($data);
        $result ? $this->success('保存成功！') : $this->error('保存失败！');
    }
}